package com.jobsys.pageprocesser;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.jobsys.dbprocess.Dbjob;
import com.jobsys.model.MJobinfo;
import com.jobsys.utils.xx;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class QianChengWuYouPage implements PageProcessor {
	// First part:抓取包括编码、抓取间隔、重试次数等
	private Site site = Site.me().setDomain("search.51job.com").setRetryTimes(2).setTimeOut(10000).setSleepTime(5000)
			.addHeader("Connection", "keep-alive")
			.addHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36")
			.addHeader("X-Requested-With", "XMLHttpRequest")
			.addHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
			.addHeader("accept-encoding", "gzip, deflate, sdch, br").addHeader("Accept-Language", "zh-CN,zh;q=0.8")
			.addHeader("upgrade-insecure-requests", "1").addHeader("Content-Type", "application/x-www-form-urlencoded");

	private Logger logger = Logger.getLogger(QianChengWuYouPage.class);

	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}

	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		try {
			logger.info("开始处理页面...");
			//
			List<Selectable> divs = page.getHtml().xpath("//div[@class='dw_table']/div[@class='el']").nodes();

			SimpleDateFormat st = new SimpleDateFormat("MM-dd");
			SimpleDateFormat st2 = new SimpleDateFormat("yyyy-MM-dd");
			String todaystr = st.format(new Date());
			Date date = st2.parse(st2.format(new Date()));

			boolean next = true;
			String source = "前程无忧";
			String sourcelink = "http://www.51job.com/?from=baidupz";

			// System.out.println(page.getRawText());
			for (int i = 1; i < divs.size(); i++) {
				String title = divs.get(i).xpath("//p[@class='t1 ']/span/a/@title").get();
				String titlelink = divs.get(i).xpath("//p[@class='t1 ']/span/a/@href").get();
				String position = title;
				String company = divs.get(i).xpath("//span[@class='t2']/a/@title").get();
				String address = divs.get(i).xpath("//span[@class='t3']/text()").get();
				String salary = divs.get(i).xpath("//span[@class='t4']/text()").get();
				String pulishtime = divs.get(i).xpath("//span[@class='t5']/text()").get();
				if (!todaystr.equals(pulishtime)) {
					// next = false;
					continue;
				}

				// 存入数据库中
				MJobinfo info = new MJobinfo();
				info.set("source", source);
				info.set("sourcelink", sourcelink);
				info.set("title", title);
				info.set("titlelink", titlelink);
				info.set("position", position);
				info.set("company", company);
				info.set("city", address);
				info.set("salary", salary);
				info.set("publishtime", date);

				if (Dbjob.isExist(titlelink)) {
					continue;
				} else {
					Dbjob.insert(info);
				}
			}

			if (next) {
				List<Selectable> lis = page.getHtml().xpath("//div[@class='p_in']/ul/li[@class='bk']").nodes();
				String nextUrl = lis.get(1).xpath("//a/@href").get();
				if (!xx.isEmpty(nextUrl)) {
					page.addTargetRequest(nextUrl);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
		}
	}

}
